package com.lijj.springboot_jsp_shiro.service.impl;

import com.lijj.springboot_jsp_shiro.dao.UserDAO;
import com.lijj.springboot_jsp_shiro.entity.Perms;
import com.lijj.springboot_jsp_shiro.entity.Role;
import com.lijj.springboot_jsp_shiro.entity.User;
import com.lijj.springboot_jsp_shiro.service.UserService;
import com.lijj.springboot_jsp_shiro.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    //根据角色id查询权限集合
    public List<Perms> findPermsByRoleId(String id) {
        return userDAO.findPermsByRoleId(id);
    }

    //根据用户名查询所有角色
    @Override
    public User findRolesByUserName(String username) {
        return userDAO.findRolesByUserName(username);
    }

    //注册用户方法
    @Override
    public void register(User user) {
        //处理业务调用dao
        //明文密码进行md5 + salt + hash散列
        //1. 生成随机盐
        String salt = SaltUtils.getSalt(8);
        //2. 将随机盐保存到数据库
        user.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());
        userDAO.save(user);
    }

    //根据用户名查询用户信息
    @Override
    public User findByUserName(String username) {
        return userDAO.findByUserName(username);
    }


}
